package yxy.game.pm2.active.command.flop;

import yxy.apple.logger.Logger;
import yxy.cherry.data.bean.actflopstagereward;
import yxy.cherry.data.bean.actflopturntable;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_翻牌;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.active.module.actdata.ActDataCenter;
import yxy.game.pm2.active.module.subordinateclass.flop.FlopActive;
import yxy.game.pm2.active.module.subordinateclass.flop.FlopActiveTask;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Item;
import yxy.game.pm2.bean.currency.CurrencyType;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;
import java.util.Map;

/**
 * 翻牌 - 活跃任务
 * @author Dell
 *
 */
@MessageType(proto = Command.ActFlopCardActiveInfo, classtype = ActiveMessage.ActFlopCardActiveInfoRequest.class)
public class CMD_Active_翻牌_活跃任务_Info extends Command<ActiveMessage.ActFlopCardActiveInfoRequest> {

	private static final Logger logger = Logger.create(CMD_Active_翻牌_活跃任务_Info.class);
	
	@Override
	public void execute(MessageSource messageSource, ActiveMessage.ActFlopCardActiveInfoRequest request) throws Exception {
		String playercode = messageSource.getTag();
		ActiveMessage.ActFlopCardActiveInfoResponse.Builder response = ActiveMessage.ActFlopCardActiveInfoResponse.newBuilder();
		
		ActiveModule_翻牌 am = ActiveModule_翻牌.get(playercode,false);
		if(am == null) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		if(!am.running()) {
			send(messageSource, response, Result.ActNoOpen);
			return;
		}

		FlopActive fa = am.getFa();
		
		//活跃任务
		for(FlopActiveTask fat : fa.getFats()) {
			ActiveMessage.FlopActiveTask.Builder build = ActiveMessage.FlopActiveTask.newBuilder();
			build.setId(fat.getId());
			build.setTaskid(fat.getTaskid());
			
			String taskcode = fat.getTaskcode();
			Task taskbean = Task.get(playercode, taskcode);
			build.setProgress(taskbean.getProgress());
			int canget = taskbean.isReward() ? DrawStatus.hadget.getStatus() : taskbean.isFinish() ? DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
			build.setCanget(canget);

			response.addFats(build.build());
		}
		
		//活跃任务完成数宝箱
		response.setFinishtasknum(fa.getFinishactivetasknum());
		Map<String, actflopstagereward> afsrmap = ActDataCenter.getAllData(actflopstagereward.class, playercode);
		for(actflopstagereward data : afsrmap.values()) {
			ActiveMessage.FlopActiveTaskNumBox.Builder build = ActiveMessage.FlopActiveTaskNumBox.newBuilder();
			build.setId(data.getId());
			build.setCondition(data.getCondition());

			Currency cur = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
			Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(cur);
			build.addAllCurs(items);
			
			int canget = fa.getFinishactivetasknumboxgetids().contains(data.getId()) ? DrawStatus.hadget.getStatus() : fa.getFinishactivetasknum() >= data.getCondition() ?
					DrawStatus.canget.getStatus() : DrawStatus.cannotget.getStatus();
			build.setCanget(canget);
			
			response.addFatnbs(build.build());
		}

		//夺宝
		Map<String, actflopturntable> afttmap = ActDataCenter.getAllData(actflopturntable.class, playercode);
		for(actflopturntable aftt : afttmap.values()) {
			ActiveMessage.FlopTurn.Builder build = ActiveMessage.FlopTurn.newBuilder();
			build.setPosition(aftt.getPosition());
			
			Currency cur = Currency.create(aftt.getRewardtype(), aftt.getRewardid(), aftt.getRewardnum());
			PackageMessage.Item item = new CurrencyHandle().packItem(cur);
			build.setCur(item);
			
			response.addFts(build.build());
		}
		
		Currency cur = Item.get(playercode, CurrencyType.CT_物品, 1040);
		response.setItemnum(cur.getNumber());
 		
		response.setStart(am.getTimeline().getStartTime().getMillis());
		response.setEnd(am.getTimeline().getEndTime().getMillis());
		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, ActiveMessage.ActFlopCardActiveInfoResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}
